Page({
  data: {
    artId: "",
    comId: "",
    articleData: {},
    comments: [],
    singleComment: [],
    replys: [],
    replyCount: "",
    isWriteShow: false,
    isReplyShow: false,
  },
  onLoad(params) {
    this.getArticle(params.id);
    this.getComments(params.id);
    this.setData({
      artId: params.id,
    });
  },

  async followAuthor() {
    if (!getApp().token) return wx.utils.toast("请先登录哦");
    if (this.data.articleData.is_followed) {
      await wx.http.delete(
        "/v1_0/user/followings/" + this.data.articleData.aut_id
      );
      wx.utils.toast("取消关注");
    } else {
      await wx.http.post("/v1_0/user/followings", {
        target: this.data.articleData.aut_id,
      });
      wx.utils.toast("添加关注成功！");
    }
    this.setData({
      "articleData.is_followed": !this.data.articleData.is_followed,
    });
  },

  async getArticle(id) {
    const res = await wx.http.get(`/v1_0/articles/${id}`);
    console.log("文章详情", res);
    const articleData = res.data.data;
    articleData.pastTime = wx.utils.getPastTime(articleData.pubdate);
    this.setData({
      articleData: articleData,
    });
  },

  async getComments(id) {
    const {
      data: { data },
    } = await wx.http.get("/v1_0/comments", {
      type: "a",
      source: id,
      limit: 20,
    });
    console.log("评论详情", data.results);
    data.results.forEach(
      (item) => (item.pastTime = wx.utils.getPastTime(item.pubdate))
    );
    this.setData({
      comments: data.results,
    });
  },

  async getSingleComment(id) {
    await this.getComments(this.data.artId);
    const singleComment = this.data.comments.filter(
      (item) => item.com_id === id
    );
    this.setData({
      singleComment,
    });
  },

  openWrite() {
    if (!getApp().token) return wx.utils.toast("请先登录哦");
    this.setData({
      isWriteShow: true,
    });
  },

  closeWrite() {
    this.setData({
      isWriteShow: false,
    });
  },

  async submitWrite({ detail }) {
    if (!getApp().token) return wx.utils.toast("请先登录哦");
    if (this.data.isReplyShow === true) {
      const res = await wx.http.post("/v1_0/comments", {
        target: this.data.comId,
        content: detail,
        art_id: this.data.artId,
      });
      if (res.data.message !== "OK") return wx.utils.toast("回复提交失败");
      this.getReplys(this.data.comId);
      this.getSingleComment(this.data.comId);
      this.closeWrite();
      wx.utils.toast("回复提交成功！");
    } else {
      const res = await wx.http.post("/v1_0/comments", {
        target: this.data.artId,
        content: detail,
      });
      if (res.data.message !== "OK") return wx.utils.toast("评论提交失败");
      this.getComments(this.data.artId);
      this.closeWrite();
      wx.utils.toast("评论提交成功！");
    }
  },

  async toggleCollect() {
    if (!getApp().token) return wx.utils.toast("请先登录哦");
    if (this.data.articleData.is_collected) {
      await wx.http.delete("/v1_0/article/collections/" + this.data.artId);
      wx.utils.toast("删除收藏");
    } else {
      await wx.http.post("/v1_0/article/collections", {
        target: this.data.artId,
      });
      wx.utils.toast("添加收藏成功！");
    }
    this.setData({
      "articleData.is_collected": !this.data.articleData.is_collected,
    });
  },

  async openReply({ detail: { comId, isReplyShow } }) {
    this.getSingleComment(comId);
    this.getReplys(comId);
    this.setData({
      comId,
      isReplyShow,
    });
  },

  closeReply() {
    this.getComments(this.data.artId);
    this.setData({
      isReplyShow: false,
    });
  },

  async getReplys(id) {
    const { data } = await wx.http.get("/v1_0/comments", {
      type: "c",
      source: id,
    });
    data.data.results.forEach(
      (item) => (item.pastTime = wx.utils.getPastTime(item.pubdate))
    );
    console.log("请求所有回复", data);
    this.setData({
      replys: data.data.results,
      replyCount: data.data.total_count,
    });
  },
});
